﻿using System;
using Microsoft.VisualStudio.Tools.Applications.Runtime;
using Word = Microsoft.Office.Interop.Word;
using Office = Microsoft.Office.Core;
using System.Windows.Forms;
using System.Collections;
using System.Collections.Generic;

using WordDoc = Microsoft.Office.Interop.Word.Document;
using PannelCreate = WordTemplate2.Pannel;
using VersionWindow = WordTemplate2.Pannel.VersionWindow;
using LettersColumnPanel = WordTemplate2.Pannel.LettersColumnPanel;
using NumberColumnPanel = WordTemplate2.Pannel.NumberColumnPanel;
using FootNoteForm = WordTemplate2.Pannel.FootNoteForm;
using PictureFootNoteForm = WordTemplate2.Pannel.PictureFootNoteForm;

namespace WordTemplate2
{
    public partial class ThisDocument
    {
        #region 点击事件
        //标准
        public void button_0_1_0_Click(Office.CommandBarButton cmdBarbutton, ref bool cancelButton)
        {
            PannelCreate.Pannel_1_Main mainwin = new PannelCreate.Pannel_1_Main();
            mainwin.Visible = true;
        }

        public void button_0_1_2_Click(Office.CommandBarButton cmdBarbutton, ref bool cancelButton)
        {
            OpenNewTemplate("论证报告.doc");
        }

        public void button_0_1_4_Click(Office.CommandBarButton cmdBarbutton, ref bool cancelButton)
        {
            OpenNewTemplate("编制说明.doc");
        }

        public void button_0_1_5_Click(Office.CommandBarButton cmdBarbutton, ref bool cancelButton)
        {
            OpenNewTemplate("报批书.doc");
        }

        public void button_0_1_8_Click(Office.CommandBarButton cmdBarbutton, ref bool cancelButton)
        {
            OpenNewTemplate("意见汇总处理表.doc");
        }

        public void button_0_1_12_Click(Office.CommandBarButton cmdBarbutton, ref bool cancelButton)
        {
            if (!Globals.ThisDocument.CheckBookmarkExist("mbookmark4"))
                return;
            //ReplaceHeaderFooterLink();
            //RebuildHeaderFooter();
        }

        public void button_0_2_0_Click(Office.CommandBarButton cmdBarbutton, ref bool cancelButton)
        {
            if (!CheckBookmarkExist("mbookmark4"))
            {
                MessageBox.Show("不是QRJ标准文件");
                return;
            }

            PannelCreate.Pannel_1 createwin = new PannelCreate.Pannel_1();
            createwin.is_change_cover = true;
            createwin.Visible = true;
        }
      
        public void button_0_2_1_Click(Office.CommandBarButton cmdBarbutton, ref bool cancelButton)
        {
            if (!CheckBookmarkExist("mbookmark4"))
            {
                MessageBox.Show("不是QRJ标准文件");
                return;
            }

            PannelCreate.UpdateIndex updateindex = new PannelCreate.UpdateIndex();
            updateindex.Visible = true;
        }

        public void button_0_2_2_Click(Office.CommandBarButton cmdBarbutton, ref bool cancelButton)
        {
            if (!CheckBookmarkExist("mbookmark4"))
            {
                MessageBox.Show("不是QRJ标准文件");
                return;
            }

            object move = Word.WdMovementType.wdMove;
            object extend = Word.WdMovementType.wdExtend;
            object line = Word.WdUnits.wdLine;
            object character = Word.WdUnits.wdCharacter;

            object number = 1;

            if (!CheckBookmarkExist("mbookmark2"))
            {
                if (CheckBookmarkExist("mbookmark1"))
                {
                    MoveBookmarkEnd("mbookmark1");
                }
                else if (CheckBookmarkExist("mbookmark0"))
                {
                    MoveBookmarkEnd("mbookmark0");
                }
                else
                {
                    return;
                }
                
                Globals.ThisDocument.Application.Selection.MoveDown(ref line, ref number, ref move);
                Globals.ThisDocument.Application.Selection.TypeText(" ");
                Globals.ThisDocument.Application.Selection.MoveLeft(ref character, ref number, ref extend);
                object range = Globals.ThisDocument.Application.Selection.Range;
                Globals.ThisDocument.Application.ActiveDocument.Bookmarks.Add("mbookmark2", ref range);
                ReplaceBookmarkContentWithFileBookmark("mbookmark2", "标准模板.doc");

                MoveBookmarkStart("mbookmark2");

            }
            else
            {
                DialogResult result = MessageBox.Show("前言已经存在了，是否重新生成？","系统提示",MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk);
                if(result == DialogResult.OK)
                {            
                    ReplaceBookmarkContentWithFileBookmark("mbookmark2", "标准模板.doc");
                    MoveBookmarkStart("mbookmark2");
                }
            }
            
        }
        public void button_0_2_3_Click(Office.CommandBarButton cmdBarbutton, ref bool cancelButton)
        {
            if (!CheckBookmarkExist("mbookmark4"))
            {
                MessageBox.Show("不是QRJ标准文件");
                return;
            }

            object move = Word.WdMovementType.wdMove;
            object extend = Word.WdMovementType.wdExtend;
            object line = Word.WdUnits.wdLine;
            object character = Word.WdUnits.wdCharacter;

            object number = 1;
            if (!CheckBookmarkExist("mbookmark3"))
            {
                if (CheckBookmarkExist("mbookmark2"))
                {
                    MoveBookmarkEnd("mbookmark2");
                }
                else
                {
                    return;
                }

                Globals.ThisDocument.Application.Selection.MoveDown(ref line, ref number, ref move);
                Globals.ThisDocument.Application.Selection.TypeText(" ");
                Globals.ThisDocument.Application.Selection.MoveLeft(ref character, ref number, ref extend);
                object range = Globals.ThisDocument.Application.Selection.Range;
                Globals.ThisDocument.Application.ActiveDocument.Bookmarks.Add("mbookmark3", ref range);
                ReplaceBookmarkContentWithFileBookmark("mbookmark3", "标准模板.doc");

                MoveBookmarkStart("mbookmark3");

            }
            else
            {
                DialogResult result = MessageBox.Show("引言已经存在了，是否重新生成？", "系统提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk);
                if (result == DialogResult.OK)
                {
                    ReplaceBookmarkContentWithFileBookmark("mbookmark3", "标准模板.doc");
                    MoveBookmarkStart("mbookmark3");
                }
            }
        }
        public void button_0_2_4_Click(Office.CommandBarButton cmdBarbutton, ref bool cancelButton)
        {
            if (!CheckBookmarkExist("mbookmark4"))
            {
                MessageBox.Show("不是QRJ标准文件");
                return;
            }

            PannelCreate.AddAppendix addappendix = new PannelCreate.AddAppendix();
            addappendix.Visible = true;
        }

        public void button_0_2_5_Click(Office.CommandBarButton cmdBarbutton, ref bool cancelButton)
        {
            if (!CheckBookmarkExist("mbookmark4"))
            {
                MessageBox.Show("不是QRJ标准文件");
                return;
            }

            object move = Word.WdMovementType.wdMove;
            object extend = Word.WdMovementType.wdExtend;
            object line = Word.WdUnits.wdLine;
            object character = Word.WdUnits.wdCharacter;

            object number = 1;

            if (!CheckBookmarkExist("mbookmark6"))
            {
                if (CheckBookmarkExist("mbookmark5"))
                {
                    MoveBookmarkEnd("mbookmark5");
                    object pagebreak = Word.WdBreakType.wdSectionBreakNextPage;
                    Globals.ThisDocument.Application.Selection.InsertBreak(ref pagebreak);

                    if (Globals.ThisDocument.CheckBookmarkExist("mbookmark7"))
                    {
                       
                    }
                }
                else if (CheckBookmarkExist("mbookmark4"))
                {
                    MoveBookmarkEnd("mbookmark4");
                    if (Globals.ThisDocument.CheckBookmarkExist("mbookmark7"))
                    {
                        object pagebreak = Word.WdBreakType.wdSectionBreakNextPage;
                        Globals.ThisDocument.Application.Selection.InsertBreak(ref pagebreak);
                    }
                    else
                    {
                        Globals.ThisDocument.Application.Selection.MoveDown(ref line, ref number, ref move);
                    }

                }
                else
                {
                    return;
                }

                Globals.ThisDocument.Application.Selection.TypeText(" ");
                Globals.ThisDocument.Application.Selection.MoveLeft(ref character, ref number, ref extend);

                object range = Globals.ThisDocument.Application.Selection.Range;
                Globals.ThisDocument.Application.ActiveDocument.Bookmarks.Add("mbookmark6", ref range);
                ReplaceBookmarkContentWithFileBookmark("mbookmark6", "标准模板.doc");

                //if(Globals.ThisDocument.CheckBookmarkExist("mbookmark7"))
                //{
                //    MoveBookmarkEnd("mbookmark6");
                //    object pagebreak = Word.WdBreakType.wdSectionBreakNextPage;
                //    Globals.ThisDocument.Application.Selection.InsertBreak(ref pagebreak);
                //}


                MoveBookmarkStart("mbookmark6");
                ContinueFooter();
            }
            else
            {
                DialogResult result = MessageBox.Show("参考文献已经存在了，是否重新生成？", "系统提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk);
                if (result == DialogResult.OK)
                {
                    ReplaceBookmarkContentWithFileBookmark("mbookmark6", "标准模板.doc");
                    MoveBookmarkStart("mbookmark6");
                }
            }
        }

        public void button_0_2_6_Click(Office.CommandBarButton cmdBarbutton, ref bool cancelButton)
        {
            if (!CheckBookmarkExist("mbookmark4"))
            {
                MessageBox.Show("不是QRJ标准文件");
                return;
            }

            object move = Word.WdMovementType.wdMove;
            object extend = Word.WdMovementType.wdExtend;
            object line = Word.WdUnits.wdLine;
            object character = Word.WdUnits.wdCharacter;

            object number = 1;

            if (!CheckBookmarkExist("mbookmark7"))
            {
                if (CheckBookmarkExist("mbookmark6"))
                {
                    MoveBookmarkEnd("mbookmark6");
                    object pagebreak = Word.WdBreakType.wdSectionBreakNextPage;
                    Globals.ThisDocument.Application.Selection.InsertBreak(ref pagebreak);
                    Globals.ThisDocument.Application.Selection.MoveRight(ref character, ref number, ref move);
                }
                else if (CheckBookmarkExist("mbookmark5"))
                {
                    MoveBookmarkEnd("mbookmark5");
                    object pagebreak = Word.WdBreakType.wdSectionBreakNextPage;
                    Globals.ThisDocument.Application.Selection.InsertBreak(ref pagebreak);
                    Globals.ThisDocument.Application.Selection.MoveRight(ref character, ref number, ref move);
                }
                else if (CheckBookmarkExist("mbookmark4"))
                {
                    object number2 = 2;
                    MoveBookmarkEnd("mbookmark4");
                    Globals.ThisDocument.Application.Selection.MoveDown(ref line, ref number, ref move);
                    Globals.ThisDocument.Application.Selection.MoveRight(ref character, ref number2, ref move);
                }
                else
                {
                    return;
                }

                Globals.ThisDocument.Application.Selection.TypeText(" ");
                Globals.ThisDocument.Application.Selection.MoveLeft(ref character, ref number, ref extend);
                object range = Globals.ThisDocument.Application.Selection.Range;
                Globals.ThisDocument.Application.ActiveDocument.Bookmarks.Add("mbookmark7", ref range);
                ReplaceBookmarkContentWithFileBookmark("mbookmark7", "标准模板.doc");
                MoveBookmarkStart("mbookmark7");
            }
            else
            {
                DialogResult result = MessageBox.Show("索引已经存在了，是否重新生成？", "系统提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk);
                if (result == DialogResult.OK)
                {
                    ReplaceBookmarkContentWithFileBookmark("mbookmark7", "标准模板.doc");
                    MoveBookmarkStart("mbookmark7");
                }
            }
        }

        public void button_0_2_7_Click(Office.CommandBarButton cmdBarbutton, ref bool cancelButton)
        {
            if (!CheckBookmarkExist("mbookmark4"))
            {
                MessageBox.Show("不是QRJ标准文件");
                return;
            }

            if (CheckIsInBookmarkRange("mbookmark4"))
            {
                object screen = Word.WdUnits.wdScreen;
                object move = Word.WdMovementType.wdMove;
                object extend = Word.WdMovementType.wdExtend;
                object line = Word.WdUnits.wdLine;
                object number = 1;

                object pagebreak = Word.WdBreakType.wdSectionBreakNextPage;


                {
                    Globals.ThisDocument.Application.Selection.InsertBreak(ref pagebreak);
                    int pos = Globals.ThisDocument.Application.Selection.Start;
                    Globals.ThisDocument.Application.Selection.MoveDown(ref line, ref number, ref move);
                    object r_temp = Globals.ThisDocument.Application.Selection.Range;
                    Globals.ThisDocument.Application.ActiveDocument.Bookmarks.Add("temp_landscape", ref r_temp);
                    Globals.ThisDocument.Application.Selection.Start = pos;
                    Globals.ThisDocument.Application.Selection.End = pos;

                    SelectionParseFromFileBookmark("横页.doc", "mbookmark8", true);


                }


                {
                    MoveBookmarkSelected("mbookmark8_header");
                    Globals.ThisDocument.Application.Selection.TypeText(ThisDocument.current_header);
                    Globals.ThisDocument.Application.Selection.MoveUp(ref line, ref number, ref extend);

                    int ref_count = GetBookmarkLandscapeCount() + 1;
                    String str_bookmark_name = "mbookmark8_header_" + ref_count;
                    object r_temp = Globals.ThisDocument.Application.Selection.Range;
                    Globals.ThisDocument.Application.ActiveDocument.Bookmarks.Add(str_bookmark_name, ref r_temp);
                }



                MoveBookmarkSelected("temp_landscape");
                ResetHeaderLandscape();

                Globals.ThisDocument.RemoveBookmark("temp_landscape");

                ContinueFooter();


                Globals.ThisDocument.MoveBookmarkStart("mbookmark8");

                Globals.ThisDocument.RemoveBookmark("mbookmark8");
                Globals.ThisDocument.RemoveBookmark("temp");


            }
            else
            {
                MessageBox.Show("当前位置不能插入横页。");
            }
        }

        public void button_0_2_8_Click(Office.CommandBarButton cmdBarbutton, ref bool cancelButton)
        {
            if (!CheckBookmarkExist("mbookmark4"))
            {
                MessageBox.Show("不是QRJ标准文件");
                return;
            }

            object move = Word.WdMovementType.wdMove;
            object extend = Word.WdMovementType.wdExtend;
            object story = Word.WdUnits.wdStory;
            object character = Word.WdUnits.wdCharacter;

            System.Collections.IEnumerator bookmark = GetMasterDocument().Bookmarks.GetEnumerator();
            while (bookmark.MoveNext())
            {
                Microsoft.Office.Interop.Word.Bookmark b = (Microsoft.Office.Interop.Word.Bookmark)bookmark.Current;
                if (b.Name == "mbookmark8")
                {
                    return;
                }
            }

            Globals.ThisDocument.Application.Selection.EndKey(ref story, ref move);
            Globals.ThisDocument.Application.Selection.TypeText(" ");

            SelectionParseFromTextureCenter("endline.bmp");
            Globals.ThisDocument.Application.Selection.TypeText(" ");
            object number = 3;
            Globals.ThisDocument.Application.Selection.MoveLeft(ref character, ref number, ref extend);

            
            object range = Globals.ThisDocument.Application.Selection.Range;
            Globals.ThisDocument.Application.ActiveDocument.Bookmarks.Add("mbookmark8", ref range);
            
            number = 1;
            Globals.ThisDocument.Application.Selection.MoveRight(ref character, ref number, ref move);
            
            Globals.ThisDocument.Application.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
            
        }

        public void button_0_3_0_Click(Office.CommandBarButton cmdBarbutton, ref bool cancelButton)
        {
            if (!CheckBookmarkExist("mbookmark4"))
            {
                MessageBox.Show("不是QRJ标准文件");
                return;
            }

            DialogResult result = MessageBox.Show("你真的要删除目次吗?", "系统提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk);
            if (result == DialogResult.OK)
            {
                if (CheckBookmarkExist("mbookmark1"))
                {
                    object extend = Word.WdMovementType.wdExtend;
                    object character = Word.WdUnits.wdCharacter;
                    object count = 1;

                    MoveBookmarkSelected("mbookmark1");
                    Globals.ThisDocument.ReplaceBookmarkContentWithString("mbookmark1", "");
                    Globals.ThisDocument.RemoveBookmark("mbookmar1");
                    Globals.ThisDocument.Application.Selection.MoveRight(ref character, ref count, ref extend);
                    Globals.ThisDocument.Application.Selection.Delete(ref varMissing, ref varMissing);
                }
                else
                {
                    MessageBox.Show("文件中没有目次");
                }
            }
        }

        public void button_0_3_1_Click(Office.CommandBarButton cmdBarbutton, ref bool cancelButton)
        {
            if (!CheckBookmarkExist("mbookmark4"))
            {
                MessageBox.Show("不是QRJ标准文件");
                return;
            }

            DialogResult result = MessageBox.Show("你真的要删除引言吗?", "系统提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk);
            if (result == DialogResult.OK)
            {
                if (CheckBookmarkExist("mbookmark3"))
                {
                    Globals.ThisDocument.ReplaceBookmarkContentWithString("mbookmark3", "");
                    Globals.ThisDocument.RemoveBookmark("mbookmar3");
                }
                else
                {
                    MessageBox.Show("文件中没有引言");
                }
            }
        }

        public void button_0_3_2_Click(Office.CommandBarButton cmdBarbutton, ref bool cancelButton)
        {
            if (!CheckBookmarkExist("mbookmark4"))
            {
                MessageBox.Show("不是QRJ标准文件");
                return;
            }

            //DialogResult result = MessageBox.Show("你真的要删除引言吗?", "系统提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk);
            //if (result == DialogResult.OK)
            {
                if (CheckBookmarkExist("mbookmark5"))
                {
                    object extend = Word.WdMovementType.wdExtend;
                    object character = Word.WdUnits.wdCharacter;
                    object count = 1;

                    MoveBookmarkSelected("mbookmark5");
                    Globals.ThisDocument.ReplaceBookmarkContentWithString("mbookmark5", "");
                    Globals.ThisDocument.RemoveBookmark("mbookmar5");

                    if (CheckBookmarkExist("mbookmark6") || CheckBookmarkExist("mbookmark7"))
                    {
                        Globals.ThisDocument.Application.Selection.MoveRight(ref character, ref count, ref extend);
                        Globals.ThisDocument.Application.Selection.Delete(ref varMissing, ref varMissing);
                    }
                }
                else
                {
                    MessageBox.Show("文件中没有附录");
                }
            }
        }

        public void button_0_3_3_Click(Office.CommandBarButton cmdBarbutton, ref bool cancelButton)
        {
            if (!CheckBookmarkExist("mbookmark4"))
            {
                MessageBox.Show("不是QRJ标准文件");
                return;
            }

            DialogResult result = MessageBox.Show("你真的要删除参考文献吗?", "系统提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk);
            if (result == DialogResult.OK)
            {
                if (CheckBookmarkExist("mbookmark6"))
                {
                    object extend = Word.WdMovementType.wdExtend;
                    object character = Word.WdUnits.wdCharacter;
                    object count = 1;

                    MoveBookmarkSelected("mbookmark6");
                    Globals.ThisDocument.ReplaceBookmarkContentWithString("mbookmark6", "");
                    Globals.ThisDocument.RemoveBookmark("mbookmar6");

                    if (CheckBookmarkExist("mbookmark7"))
                    {
                        Globals.ThisDocument.Application.Selection.MoveRight(ref character, ref count, ref extend);
                        Globals.ThisDocument.Application.Selection.Delete(ref varMissing, ref varMissing);
                    }
                }
                else
                {
                    MessageBox.Show("文件中没有参考文献");
                }
            }
        }

        public void button_0_3_4_Click(Office.CommandBarButton cmdBarbutton, ref bool cancelButton)
        {
            if (!CheckBookmarkExist("mbookmark4"))
            {
                MessageBox.Show("不是QRJ标准文件");
                return;
            }

            DialogResult result = MessageBox.Show("你真的要删除索引吗?", "系统提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk);
            if (result == DialogResult.OK)
            {
                if (CheckBookmarkExist("mbookmark7"))
                {
                    MoveBookmarkSelected("mbookmark7");
                    Globals.ThisDocument.ReplaceBookmarkContentWithString("mbookmark7", "");
                    Globals.ThisDocument.RemoveBookmark("mbookmar7");
                }
                else
                {
                    MessageBox.Show("文件中没有索引");
                }
            }
        }

        public void button_0_3_5_Click(Office.CommandBarButton cmdBarbutton, ref bool cancelButton)
        {
            if (!CheckBookmarkExist("mbookmark4"))
            {
                MessageBox.Show("不是QRJ标准文件");
                return;
            }

            if (CheckPageIsLandscape())
            {
                DeleteSelectionWindow();

                object extend = Word.WdMovementType.wdExtend;
                object character = Word.WdUnits.wdCharacter;
                object count = 1;
                Globals.ThisDocument.Application.Selection.MoveLeft(ref character, ref count, ref extend);
                Globals.ThisDocument.Application.Selection.Delete(ref varMissing, ref varMissing);
            }
            else
            {
                MessageBox.Show("当前页面不是横页");
            }
        }

        public void button_0_4_0_Click(Office.CommandBarButton cmdBarbutton, ref bool cancelButton)
        {
            if (!CheckBookmarkExist("mbookmark4"))
            {
                MessageBox.Show("不是QRJ标准文件");
                return;
            }

            if (!CheckBookmarkExist("mbookmark5"))
            {
                MessageBox.Show("当前文档没有附录");
                return;
            }

            MoveBookmarkStart("mbookmark5");

            object move = Word.WdMovementType.wdMove;
            object extend = Word.WdMovementType.wdExtend;
            object line = Word.WdUnits.wdLine;
            object character = Word.WdUnits.wdCharacter;

            object number = 1;
            
            List<string> list = new List<string>();
            Word.Range r = Globals.ThisDocument.GetBookmarkRange("mbookmark5");
            string last_string = "";
            do
            {
                string str = Globals.ThisDocument.Application.Selection.Range.ListFormat.ListString;
                if (str.Contains("附"))
                {
                    if (last_string != str)
                    {
                        last_string = str;
                        str = str.Replace("附 录 ", "附录");
                        list.Add(str);
                    }
                }
                Globals.ThisDocument.Application.Selection.MoveDown(ref line, ref number, ref move);
            }
            while (Globals.ThisDocument.Application.Selection.Range.End <= r.End);

            Word.Range r1 = Globals.ThisDocument.GetBookmarkRange("mbookmark4");
            foreach (string str in list)
            {
                if (!r1.Text.Contains(str))
                {
                    MessageBox.Show(str + "在正文中不存在");
                    return;
                }
            }
            
            MessageBox.Show("检查附录过程中没有发现问题");
        }

        public void button_0_5_0_Click(Office.CommandBarButton cmdBarbutton, ref bool cancelButton)
        {
            if (!CheckBookmarkExist("mbookmark4"))
            {
                MessageBox.Show("不是QRJ标准文件");
                return;
            }

            PannelCreate.CheckRefrence mainwin = new PannelCreate.CheckRefrence();
            mainwin.Visible = true;
        }

        public void button_0_7_0_Click(Office.CommandBarButton cmdBarbutton, ref bool cancelButton)
        {
            if (!CheckBookmarkExist("mbookmark4"))
            {
                MessageBox.Show("不是QRJ标准文件");
                return;
            }

            if (!CheckBookmarkExist("mbookmark4_ref"))
            {
                MessageBox.Show("找不到引用文件段落，文件可能已损坏");
                return;
            }

            object move = Word.WdMovementType.wdMove;
            object extend = Word.WdMovementType.wdExtend;
            object character = Word.WdUnits.wdCharacter;
            object number = 1;
            object gotobookmark = Word.WdGoToItem.wdGoToBookmark;
            object name = "mbookmark4_ref";
            object varMissing = System.Type.Missing;

            Globals.ThisDocument.Application.ActiveWindow.View.SeekView = Word.WdSeekView.wdSeekMainDocument;
            Globals.ThisDocument.Application.Selection.GoTo(ref gotobookmark, ref varMissing, ref varMissing, ref name);

            string text = Globals.ThisDocument.Application.Selection.Text;

            foreach (char a in text)
            {
                if (a != ' ' && a != '\r')
                {
                    DialogResult result = MessageBox.Show("引用文件已存在内容，确认删除并重新添加？", "系统提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk);
                    if (result == DialogResult.OK)
                    {
                        LoadExistRefrence();
                        PannelCreate.AddRefrence mainwin = new PannelCreate.AddRefrence();
                        mainwin.Visible = true;
                        ClearExistRefrence();
                        return;
                    }
                    else
                    {
                        Globals.ThisDocument.MoveBookmarkStart("mbookmark4_ref");
                        return;
                    }
                }
            }

            {
                PannelCreate.AddRefrence mainwin = new PannelCreate.AddRefrence();
                mainwin.Visible = true;
            }
        }
        

        public void button_1_1_0_Click(Office.CommandBarButton cmdBarbutton, ref bool cancelButton)
        {
            if (!CheckBookmarkExist("mbookmark4"))
            {
                MessageBox.Show("不是QRJ标准文件");
                return;
            }

            if (!CheckIsInBookmarkRange("mbookmark4"))
            {
                MessageBox.Show("不是正文范围内");
                return;
            }

            object style = "标准文件_章标题";
            Globals.ThisDocument.Application.Selection.ParagraphFormat.set_Style(ref style);
            Globals.ThisDocument.Application.Selection.ParagraphFormat.CharacterUnitLeftIndent = -0.5f;
            //SetListNumberFormat(2);
        }

        public void button_1_2_0_Click(Office.CommandBarButton cmdBarbutton, ref bool cancelButton)
        {
            if (!CheckBookmarkExist("mbookmark4"))
            {
                MessageBox.Show("不是QRJ标准文件");
                return;
            }
            if (!CheckIsInBookmarkRange("mbookmark4"))
            {
                MessageBox.Show("不是正文范围内");
                return;
            }

            object style = "标准文件_一级条标题";
            Globals.ThisDocument.Application.Selection.ParagraphFormat.set_Style(ref style);
            Globals.ThisDocument.Application.Selection.ParagraphFormat.CharacterUnitLeftIndent = -0.5f;
        }

        public void button_1_3_0_Click(Office.CommandBarButton cmdBarbutton, ref bool cancelButton)
        {
            if (!CheckBookmarkExist("mbookmark4"))
            {
                MessageBox.Show("不是QRJ标准文件");
                return;
            }
            if (!CheckIsInBookmarkRange("mbookmark4"))
            {
                MessageBox.Show("不是正文范围内");
                return;
            }

            object style = "标准文件_二级条标题";
            Globals.ThisDocument.Application.Selection.ParagraphFormat.set_Style(ref style);
            Globals.ThisDocument.Application.Selection.ParagraphFormat.CharacterUnitLeftIndent = -0.5f;
        }

        public void button_1_4_0_Click(Office.CommandBarButton cmdBarbutton, ref bool cancelButton)
        {
            if (!CheckBookmarkExist("mbookmark4"))
            {
                MessageBox.Show("不是QRJ标准文件");
                return;
            }
            if (!CheckIsInBookmarkRange("mbookmark4"))
            {
                MessageBox.Show("不是正文范围内");
                return;
            }

            object style = "标准文件_三级条标题";
            Globals.ThisDocument.Application.Selection.ParagraphFormat.set_Style(ref style);
            Globals.ThisDocument.Application.Selection.ParagraphFormat.CharacterUnitLeftIndent = -0.5f;
        }

        public void button_1_5_0_Click(Office.CommandBarButton cmdBarbutton, ref bool cancelButton)
        {
            if (!CheckBookmarkExist("mbookmark4"))
            {
                MessageBox.Show("不是QRJ标准文件");
                return;
            }

            if (!CheckIsInBookmarkRange("mbookmark4"))
            {
                MessageBox.Show("不是正文范围内");
                return;
            }

            object style = "标准文件_四级条标题";
            Globals.ThisDocument.Application.Selection.ParagraphFormat.set_Style(ref style);
            Globals.ThisDocument.Application.Selection.ParagraphFormat.CharacterUnitLeftIndent = -0.5f;
        }

        public void button_1_6_0_Click(Office.CommandBarButton cmdBarbutton, ref bool cancelButton)
        {
            if (!CheckBookmarkExist("mbookmark4"))
            {
                MessageBox.Show("不是QRJ标准文件");
                return;
            }

            if (!CheckIsInBookmarkRange("mbookmark4"))
            {
                MessageBox.Show("不是正文范围内");
                return;
            }

            object style = "标准文件_五级条标题";
            Globals.ThisDocument.Application.Selection.ParagraphFormat.set_Style(ref style);
            Globals.ThisDocument.Application.Selection.ParagraphFormat.CharacterUnitLeftIndent = -0.5f;
        }

        public void button_1_7_0_Click(Office.CommandBarButton cmdBarbutton, ref bool cancelButton)
        {
            if (!CheckBookmarkExist("mbookmark4"))
            {
                MessageBox.Show("不是QRJ标准文件");
                return;
            }

            if (!CheckIsInBookmarkRange("mbookmark4") && !CheckIsInBookmarkRange("mbookmark5"))
            {
                MessageBox.Show("不是正文或附录范围内");
                return;
            }

            if (Globals.ThisDocument.Application.Selection.Range.ListFormat.ListType == Word.WdListType.wdListOutlineNumbering)
            {
                int level = Globals.ThisDocument.Application.Selection.Range.ListFormat.ListLevelNumber;
                Word.ListLevel l = Globals.ThisDocument.Application.Selection.Range.ListFormat.ListTemplate.ListLevels[level];
                string style = l.LinkedStyle;

                if (style.Contains("级条标题") && style.Contains("标准文件"))
                {
                    object count = 1;
                    object line = Word.WdUnits.wdParagraph;
                    object move = Word.WdMovementType.wdMove;
                    object extend = Word.WdMovementType.wdExtend;
                    
                    int pos = Globals.ThisDocument.Application.Selection.Range.Start;
                    Globals.ThisDocument.Application.Selection.MoveUp(ref line, ref count,ref move);
                    Globals.ThisDocument.Application.Selection.MoveDown(ref line,ref count, ref extend);

                    if(pos == Globals.ThisDocument.Application.Selection.End)
                    {
                        pos ++;
                        Globals.ThisDocument.Application.Selection.Start = pos;
                        Globals.ThisDocument.Application.Selection.End = pos;
                        Globals.ThisDocument.Application.Selection.MoveUp(ref line, ref count, ref move);
                        Globals.ThisDocument.Application.Selection.MoveDown(ref line, ref count, ref extend);
                    }
                    Globals.ThisDocument.Application.Selection.Range.Font.Name = "宋体";
                    Globals.ThisDocument.Application.Selection.Start = pos;
                    Globals.ThisDocument.Application.Selection.End = pos;
                    return;
                    
                }
            }
            MessageBox.Show("只能将正文和附录中的条标题变为无标题条");
        }

        public void button_1_8_0_Click(Office.CommandBarButton cmdBarbutton, ref bool cancelButton)
        {
            if (!CheckBookmarkExist("mbookmark4"))
            {
                MessageBox.Show("不是QRJ标准文件");
                return;
            }

            if (!CheckIsInBookmarkRange("mbookmark4") && !CheckIsInBookmarkRange("mbookmark5"))
            {
                MessageBox.Show("不在正文范围内，不能执行此操作");
                return;
            }
            //object style = "标准文件_正文图标题";
            //object count = 1;
            //object line = Word.WdUnits.wdParagraph;
            //object move = Word.WdMovementType.wdMove;
            //object extend = Word.WdMovementType.wdExtend;

            //int pos = Globals.ThisDocument.Application.Selection.Range.Start;
            //Globals.ThisDocument.Application.Selection.MoveUp(ref line, ref count, ref move);
            //Globals.ThisDocument.Application.Selection.MoveDown(ref line, ref count, ref extend);
            //Globals.ThisDocument.Application.Selection.set_Style(ref style);
            //Globals.ThisDocument.Application.Selection.Start = pos;
            //Globals.ThisDocument.Application.Selection.End = pos;

            object style = "标准文件_正文图标题";
            Globals.ThisDocument.Application.Selection.ParagraphFormat.set_Style(ref style);
        }

        public void button_1_9_0_Click(Office.CommandBarButton cmdBarbutton, ref bool cancelButton)
        {
            if (!CheckBookmarkExist("mbookmark4"))
            {
                MessageBox.Show("不是QRJ标准文件");
                return;
            }

            if (!CheckIsInBookmarkRange("mbookmark4") && !CheckIsInBookmarkRange("mbookmark5"))
            {
                MessageBox.Show("不在正文范围内，不能执行此操作");
                return;
            }
            //object style = "标准文件_正文表标题";
            //object count = 1;
            //object line = Word.WdUnits.wdParagraph;
            //object move = Word.WdMovementType.wdMove;
            //object extend = Word.WdMovementType.wdExtend;

            //int pos = Globals.ThisDocument.Application.Selection.Range.Start;
            //Globals.ThisDocument.Application.Selection.MoveUp(ref line, ref count, ref move);
            //Globals.ThisDocument.Application.Selection.MoveDown(ref line, ref count, ref extend);
            //Globals.ThisDocument.Application.Selection.set_Style(ref style);
            //Globals.ThisDocument.Application.Selection.Start = pos;
            //Globals.ThisDocument.Application.Selection.End = pos;

            object style = "标准文件_正文表标题";
            Globals.ThisDocument.Application.Selection.ParagraphFormat.set_Style(ref style);
        }

        public void button_1_10_0_Click(Office.CommandBarButton cmdBarbutton, ref bool cancelButton)
        {
            object str = (object)"标准文件_正文公式";
            Globals.ThisDocument.Application.Selection.ParagraphFormat.set_Style(ref str);
            Globals.ThisDocument.Application.Selection.TypeText("………………………………(");


            object fieldtype = Word.WdFieldType.wdFieldEmpty;
            object text = "AUTONUM";
            Globals.ThisDocument.Application.Selection.Fields.Add(Globals.ThisDocument.Application.Selection.Range, ref fieldtype, ref text, ref varFalseValue);


            Globals.ThisDocument.Application.Selection.TypeText(")");
            Globals.ThisDocument.Application.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;
            Globals.ThisDocument.Application.Selection.TypeParagraph();
            Globals.ThisDocument.Application.Selection.TypeText("式中：");

            str = (object)"标准文件_段";
            Globals.ThisDocument.Application.Selection.ParagraphFormat.set_Style(ref str);
        }

        public void button_1_11_0_Click(Office.CommandBarButton cmdBarbutton, ref bool cancelButton)
        {
            if (!CheckBookmarkExist("mbookmark4"))
            {
                MessageBox.Show("不是QRJ标准文件");
                return;
            }

            object style = "标准文件_段";
            Globals.ThisDocument.Application.Selection.ParagraphFormat.set_Style(ref style);
        }

        public void button_1_12_0_Click(Office.CommandBarButton cmdBarbutton, ref bool cancelButton)
        {
            if (!CheckBookmarkExist("mbookmark4"))
            {
                MessageBox.Show("不是QRJ标准文件");
                return;
            }
            LettersColumnPanel p = new LettersColumnPanel();
            p.Visible = true;
        }

        public void button_1_13_0_Click(Office.CommandBarButton cmdBarbutton, ref bool cancelButton)
        {
            if (!CheckBookmarkExist("mbookmark4"))
            {
                MessageBox.Show("不是QRJ标准文件");
                return;
            }
            NumberColumnPanel p = new NumberColumnPanel();
            p.Visible = true;
        }

        public void button_1_14_0_Click(Office.CommandBarButton cmdBarbutton, ref bool cancelButton)
        {
            if (!CheckBookmarkExist("mbookmark4"))
            {
                MessageBox.Show("不是QRJ标准文件");
                return;
            }

            Word.Range r = Globals.ThisDocument.Application.Selection.Range;
            r.Start = r.End;
            object range_mark = r;
            object type_bookmark = Word.WdGoToItem.wdGoToBookmark;
            object type_footnotes = Word.WdGoToItem.wdGoToFootnote;
            object name = "脚注";

            object move = Word.WdMovementType.wdMove;
            object extend = Word.WdMovementType.wdExtend;
            object character = Word.WdUnits.wdCharacter;
            object count = 1;
            Word.Bookmark book_mark = GetMasterDocument().Bookmarks.Add("脚注", ref range_mark);

            try
            {
                GetMasterDocument().Footnotes.Add(r, ref varMissing, ref varMissing);
                Globals.ThisDocument.Application.Selection.MoveLeft(ref character, ref count, ref move);
                Globals.ThisDocument.Application.Selection.TypeText(") ");
                Globals.ThisDocument.Application.Selection.GoTo(ref type_footnotes, ref varMissing, ref varMissing, ref varMissing);
                Globals.ThisDocument.Application.Selection.MoveRight(ref character, ref count, ref extend);
                Globals.ThisDocument.Application.Selection.Font.Superscript = 0;
                Globals.ThisDocument.Application.Selection.GoTo(ref type_bookmark, ref varMissing, ref varMissing, ref name);

                Globals.ThisDocument.Application.Selection.MoveRight(ref character, ref count, ref move);
                Globals.ThisDocument.Application.Selection.TypeText(")");
                Globals.ThisDocument.Application.Selection.MoveLeft(ref character, ref count, ref extend);
                object style = "脚注引用,标准文件_脚注引用";
                Globals.ThisDocument.Application.Selection.set_Style(ref style);
                Globals.ThisDocument.Application.Selection.Start = Globals.ThisDocument.Application.Selection.End;
            }
            catch
            {
                MessageBox.Show("脚注，尾注只能添加到文档部分！");
            }
           
            book_mark.Delete();
        }

        public void button_1_14_1_Click(Office.CommandBarButton cmdBarbutton, ref bool cancelButton)
        {
            if (!CheckBookmarkExist("mbookmark4"))
            {
                MessageBox.Show("不是QRJ标准文件");
                return;
            }

            FootNoteForm p = new FootNoteForm();
            p.Visible = true;

        }

        public void button_1_14_2_Click(Office.CommandBarButton cmdBarbutton, ref bool cancelButton)
        {
            if (!CheckBookmarkExist("mbookmark4"))
            {
                MessageBox.Show("不是QRJ标准文件");
                return;
            }

            PictureFootNoteForm p = new PictureFootNoteForm();
            p.Visible = true;

        }

        public void button_1_14_3_Click(Office.CommandBarButton cmdBarbutton, ref bool cancelButton)
        {
            if (!CheckBookmarkExist("mbookmark4"))
            {
                MessageBox.Show("不是QRJ标准文件");
                return;
            }

            PictureFootNoteForm p = new PictureFootNoteForm();
            p.foot_note_type = 1;
            p.Visible = true;

        }

        public void button_1_15_0_Click(Office.CommandBarButton cmdBarbutton, ref bool cancelButton)
        {
            if (!CheckBookmarkExist("mbookmark4"))
            {
                MessageBox.Show("不是QRJ标准文件");
                return;
            }
           
            object style = "标准文件_示例：";
            Globals.ThisDocument.Application.Selection.ParagraphFormat.set_Style(ref style);
            SetParagraphSpecial();
            Globals.ThisDocument.Application.Selection.ParagraphFormat.TabStops.ClearAll();
            GetMasterDocument().DefaultTabStop = Globals.ThisDocument.Application.CentimetersToPoints(0);
        }

        public void button_1_16_0_Click(Office.CommandBarButton cmdBarbutton, ref bool cancelButton)
        {
            if (!CheckBookmarkExist("mbookmark4"))
            {
                MessageBox.Show("不是QRJ标准文件");
                return;
            }

            NumberColumnPanel p = new NumberColumnPanel();
            p.number_board_type = 1;
            p.Visible = true;
            
      
        }

        public void button_1_17_0_Click(Office.CommandBarButton cmdBarbutton, ref bool cancelButton)
        {
            if (!CheckBookmarkExist("mbookmark4"))
            {
                MessageBox.Show("不是QRJ标准文件");
                return;
            }

            object style = "标准文件_注：";
            Globals.ThisDocument.Application.Selection.ParagraphFormat.set_Style(ref style);
            SetParagraphSpecial();
            Globals.ThisDocument.Application.Selection.ParagraphFormat.TabStops.ClearAll();
            GetMasterDocument().DefaultTabStop = Globals.ThisDocument.Application.CentimetersToPoints(0);
        }

        public void button_1_18_0_Click(Office.CommandBarButton cmdBarbutton, ref bool cancelButton)
        {
            if (!CheckBookmarkExist("mbookmark4"))
            {
                MessageBox.Show("不是QRJ标准文件");
                return;
            }

            NumberColumnPanel p = new NumberColumnPanel();
            p.number_board_type = 2;
            p.Visible = true;
        } 
        
        public void button_2_1_0_Click(Office.CommandBarButton cmdBarbutton, ref bool cancelButton)
        {
            if (!CheckBookmarkExist("mbookmark4"))
            {
                MessageBox.Show("不是QRJ标准文件");
                return;
            }
            if (!CheckIsInBookmarkRange("mbookmark5"))
            {
                MessageBox.Show("不是附录范围内");
                return;
            }

            //object style = "标准文件_附录标识";
            //Globals.ThisDocument.Application.Selection.ParagraphFormat.set_Style(ref style);
            SetAppendixListNumberFormat(2);
        }

        public void button_2_2_0_Click(Office.CommandBarButton cmdBarbutton, ref bool cancelButton)
        {
            if (!CheckBookmarkExist("mbookmark4"))
            {
                MessageBox.Show("不是QRJ标准文件");
                return;
            }
            if (!CheckIsInBookmarkRange("mbookmark5"))
            {
                MessageBox.Show("不是附录范围内");
                return;
            }

            SetAppendixListNumberFormat(3);
        }

        public void button_2_3_0_Click(Office.CommandBarButton cmdBarbutton, ref bool cancelButton)
        {
            if (!CheckBookmarkExist("mbookmark4"))
            {
                MessageBox.Show("不是QRJ标准文件");
                return;
            }
            if (!CheckIsInBookmarkRange("mbookmark5"))
            {
                MessageBox.Show("不是附录范围内");
                return;
            }

            SetAppendixListNumberFormat(4);
        }

        public void button_2_4_0_Click(Office.CommandBarButton cmdBarbutton, ref bool cancelButton)
        {
            if (!CheckBookmarkExist("mbookmark4"))
            {
                MessageBox.Show("不是QRJ标准文件");
                return;
            }
            if (!CheckIsInBookmarkRange("mbookmark5"))
            {
                MessageBox.Show("不是附录范围内");
                return;
            }

            SetAppendixListNumberFormat(5);
        }

        public void button_2_5_0_Click(Office.CommandBarButton cmdBarbutton, ref bool cancelButton)
        {
            if (!CheckBookmarkExist("mbookmark4"))
            {
                MessageBox.Show("不是QRJ标准文件");
                return;
            }
            if (!CheckIsInBookmarkRange("mbookmark5"))
            {
                MessageBox.Show("不是附录范围内");
                return;
            }

            SetAppendixListNumberFormat(6);
        }

        public void button_2_6_0_Click(Office.CommandBarButton cmdBarbutton, ref bool cancelButton)
        {
            if (!CheckBookmarkExist("mbookmark4"))
            {
                MessageBox.Show("不是QRJ标准文件");
                return;
            }
            if (!CheckIsInBookmarkRange("mbookmark5"))
            {
                MessageBox.Show("不是附录范围内");
                return;
            }

            SetAppendixListNumberFormat(7);
        }

        public void button_2_7_0_Click(Office.CommandBarButton cmdBarbutton, ref bool cancelButton)
        {
            if (!CheckBookmarkExist("mbookmark4"))
            {
                MessageBox.Show("不是QRJ标准文件");
                return;
            }
            if (!CheckIsInBookmarkRange("mbookmark5"))
            {
                MessageBox.Show("不是附录范围内");
                return;
            }

          

            Globals.ThisDocument.Application.Selection.Range.ListFormat.RemoveNumbers(ref varMissing);

            string mark = GetAppendixText();
 
            string str = "图" + mark + ".";
            Globals.ThisDocument.Application.Selection.TypeText(str);
            
            object style = "标准文件_附录图标题";
            Globals.ThisDocument.Application.Selection.ParagraphFormat.set_Style(ref style);
        }

        public void button_2_8_0_Click(Office.CommandBarButton cmdBarbutton, ref bool cancelButton)
        {
            if (!CheckBookmarkExist("mbookmark4"))
            {
                MessageBox.Show("不是QRJ标准文件");
                return;
            }
            if (!CheckIsInBookmarkRange("mbookmark5"))
            {
                MessageBox.Show("不是附录范围内");
                return;
            }

            Globals.ThisDocument.Application.Selection.Range.ListFormat.RemoveNumbers(ref varMissing);

            string mark = GetAppendixText();

            string str = "表" + mark + ".";
            Globals.ThisDocument.Application.Selection.TypeText(str);
          
            object style = "标准文件_附录表标题";
            Globals.ThisDocument.Application.Selection.ParagraphFormat.set_Style(ref style);
        }

        public void button_2_9_0_Click(Office.CommandBarButton cmdBarbutton, ref bool cancelButton)
        {
            if (!CheckBookmarkExist("mbookmark4"))
            {
                MessageBox.Show("不是QRJ标准文件");
                return;
            }
            if (!CheckIsInBookmarkRange("mbookmark5"))
            {
                MessageBox.Show("不是附录范围内");
                return;
            }

            object str = (object)"标准文件_附录公式";
            Globals.ThisDocument.Application.Selection.ParagraphFormat.set_Style(ref str);


            string mark = GetAppendixText();

            string text = "………………………………(" + mark + ".)";


            Globals.ThisDocument.Application.Selection.TypeText(text);

            Globals.ThisDocument.Application.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;
            Globals.ThisDocument.Application.Selection.TypeParagraph();
            Globals.ThisDocument.Application.Selection.TypeText("式中：");

            str = (object)"标准文件_段";
            Globals.ThisDocument.Application.Selection.ParagraphFormat.set_Style(ref str);
        }

        //打开文件
        public void OpenFileBtn_Click(Office.CommandBarButton cmdBarbutton, ref bool cancelButton)
        {
            System.Windows.Forms.OpenFileDialog dialog = new System.Windows.Forms.OpenFileDialog();
            if(dialog.ShowDialog() == DialogResult.OK)
            {
                string filename = dialog.FileName.Replace("\\", "/");
                CopyDataFromFileFullPath(filename);

                Word.Sections sections = GetMasterDocument().Sections;
                foreach (Word.Section section in sections)
                {
                    foreach (Word.HeaderFooter hf in section.Headers)
                    {
                        Word.Range r1 = hf.Range;

                        do
                        {
                            if (r1.Text.Contains("Q/RJ"))
                            {
                                ThisDocument.current_header = r1.Text;
                                return;
                            }

                            r1 = r1.NextStoryRange;
                        } while (r1 != null);
                    }
                }
            }
        }

        //public void FileSave_Click(Office.CommandBarButton cmdBarbutton, ref bool cancelButton)
        //{
        //    SaveFileDialog saveFileDialog1 = new SaveFileDialog();

        //    //设置文件类型   
        //    saveFileDialog1.Filter = "doc files(*.doc)|*.doc";

        //    //设置默认文件类型显示顺序   
        //    saveFileDialog1.FilterIndex = 2;

        //    //保存对话框是否记忆上次打开的目录   
        //    saveFileDialog1.RestoreDirectory = true;

        //    if (saveFileDialog1.ShowDialog() == DialogResult.OK)
        //    {
        //        //获得文件路径   
        //        string localFilePath = saveFileDialog1.FileName.ToString().Replace("\\", "/");

        //        Globals.ThisDocument.SaveTempFile(localFilePath);

        //        if (ServerDocument.IsCustomized(localFilePath))
        //        {
        //            ServerDocument.RemoveCustomization(localFilePath);
        //        }
        //    }

        //}


        


        public void OpenVersionWindow(Office.CommandBarButton cmdBarbutton, ref bool cancelButton)
        {
            VersionWindow version = new VersionWindow();
            version.Visible = true;
        }

        
#endregion
    }
}
